/**
 * 
 */
package com.mypackage.LocationPattern;

import java.util.Calendar;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

/**
 * @author Shawn
 *
 */
public class LocationService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate(){
		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		      // Called when a new location is found by the network location provider.
		    	
		      double latitude = location.getLatitude();
		      double longitude = location.getLongitude();
		      int hour = Calendar.HOUR;
		      int minute = Calendar.MINUTE;
		      int day = Calendar.DAY_OF_WEEK;
		      
		      GeoStamp gs = new GeoStamp(latitude, longitude, hour, minute, day);
		      
		      boolean saved = gs.save();
		      if (saved){
		    	  Toast.makeText(LocationService.this, R.string.location_saved, Toast.LENGTH_LONG);
		      }
		      else{
		    	  Toast.makeText(LocationService.this,R.string.location_not_saved, Toast.LENGTH_LONG);
		      }
		      
		      stopSelf();
		    }

		    public void onStatusChanged(String provider, int status, Bundle extras) {}

		    public void onProviderEnabled(String provider) {}

		    public void onProviderDisabled(String provider) {}

		  };

		// Register the listener with the Location Manager to receive location updates
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30*1000, 0, locationListener);
	}

}
